<?php
class RegionsController extends Controller
{
	protected $adapter = null;
	
	public function init()
	{
		$this->adapter = V::getAdapter();
	}

	/**
	 * 向 sp_regions 表插入100条记录
	 */
	public function addAction()
	{	
		for ($i=0; $i<10; $i++)
		{
			$region_name = mt_rand(0, 1) === 1 ? 'AAA' : (mt_rand(0, 1) === 1 ? 'BBB' : 'CCC'); 
			$region = array(
				'region_name' => $region_name,
				'population' => mt_rand(1, 100) * 10000,
				'region_area' => mt_rand(1, 100) * 10000,
			);
			
			$this->adapter->table('regions', 'sp_')->fields($region)->insert();
			echo $this->adapter->getSql(), '<br/>';
		}
		exit;
	}
	
	public function groupAction()
	{
		$rows = $this->adapter->fields('region_name')->fields(array('population', 'region_area'), '', 'SUM')
		->table('regions', 'sp_')->group('region_name')->having(array('SUM(region_area) > ?', 1000000))->select();
		
		echo '<pre>';
		echo $this->adapter->getSql(), '<br/>';
		print_r($rows);
		exit;
	}
} 
